package com.itsc.util.page;

public class PageUtils {
	// 每页显示记录数
		private static Integer pageSize;
		// 当前页
		private static Integer currentPage;
		// 起点页
		private static Integer beginIndex;
		// 总页数
		private static Integer totalPage;
		// 总记录数
		private static Integer total;
		// 是否有上一页
		private static boolean hasPrePage;
		// 是否有下一页
		private static boolean hasNextPage;
		static {
			currentPage = 1;
		}
		/**
		 * 初始化
		 * @param count 总记录数
		 * @param size 每页显示记录数
		 */
		public static void initParamter(int count, int size) {
			total = count;
			pageSize = size;
			currentPage = 1;
			beginIndex = (currentPage - 1) * pageSize;
			if (total % pageSize == 0) {
				totalPage = total / pageSize;
			} else {
				totalPage = total / pageSize + 1;
			}
			if (currentPage >= totalPage) {
				hasNextPage = false;
			} else {
				hasNextPage = true;
			}
			if ((currentPage - 1) > 0) {
				hasPrePage = true;
			} else {
				hasPrePage = false;
			}
		}
		/**
		 * 设置当前页
		 * @param curr 当前页
		 */
		public static void setCurrentPage(int curr) {
			currentPage = curr;
			beginIndex = (currentPage - 1) * pageSize;
			if ((currentPage - 1) > 0) {
				hasPrePage = true;
			} else {
				hasPrePage = false;
			}
			if (currentPage >= totalPage) {
				hasNextPage = false;
			} else {
				hasNextPage = true;
			}
		}
		
		public static Page getPage() {
			Page page = new Page();
			page.setBeginIndex(beginIndex);
			page.setCurrentPage(currentPage);
			page.setHasNextPage(hasNextPage);
			page.setHasPrePage(hasPrePage);
			page.setPageSize(pageSize);
			page.setTotalPage(totalPage);
			page.setTotal(total);
			return page;
		}
}
